// pages/my/refundDetail/refundDetail.js
var app = getApp()
Page({
  data: {
    goodsInfoShow: true,
    goodsPriceShow: false,
    status:'',//审核状态 -1拒绝 0未审核 1审核通过
    refundState:'',//退款状态 -1拒退款 0未退款 1已退款
    productStatus:'',//货物状态 0已收到货 1未收到货 2已发货
    afterNo: "",
    applyCount: 0,
    applyTime: "",
    receiveType: 1,
    receiveName: "",
    refundCarNo:"",
    refundPeople: "",
    refundPhone: "",
    refundReason:"",
    refundPrice: 0,
    orderNo: "",
    logisticsCompany:'',
    logisticsNo:'',
    auditWhy:'',
    goods: {},
    addressInfo: {},
    logistics: [{
     info: "",
     time: "",
     isNew: true,
     isLast: false
   }],
   logisticsList: [
    // {
    //   info: "新大洲门卫已签收，签收人：前台  感谢您使用圆通快递，期待再次为您服务！",
    //   time: "2018-07-31   19:00",
    //   isNew: true,
    //   isLast: false
    // },{
    //     info: "从苏州高新站入库",
    //     time: "2018-07-31   19:00",
    //     isNew: true,
    //     isLast: false
    // },{
    //     info: "订单商品已发送给圆通快递",
    //     time: "2018-07-31   19:00",
    //     isNew: true,
    //     isLast: false
    // }
    ],
    refundStatus:''
  },
  writeLogistics:function(e){
    wx.navigateTo({
      url: '../writeLogistics/writeLogistics?returnId='+e.currentTarget.dataset.return,
    })
  },
  logisticsInfos:function(){
    wx.navigateTo({
      url: '../logistics/logistics?logisticType="第三方物流"'+'&returnsId='+this.data.returnsId,
    })
  },
  onLoad: function(options) {
    // var refundStatus = options.refundStatus
    // var refundStatus = 1
    let returnsId = options.returnsId
    // let returnsId = 9
    this.getAfterDetail(returnsId)
    this.setData({
      // refundStatus,
      returnsId
    })
  },
  onShow(){
    this.getLogisticInfo(this.data.logisticsCompany,this.data.logisticsNo)
    this.getAfterDetail(this.data.returnsId)
  },
  getAfterDetail(returnsId){
    let data = {
      returnsId
    }
    app.request('wx_app/after/sale/detail',data,'GET').then(res=>{
      console.log(res)
      this.setData({
        ['goods.name']:res.data.returns_info.productName,
        ['goods.url']:res.data.returns_info.productPic,
        ['goods.num']:res.data.returns_info.amount,
        ['goods.price']:res.data.returns_info.price,
        ['goods.type']:res.data.returns_info.versionDesc,
        ['goods.companyName']:res.data.returns_info.productTag,
        ['goods.color']:res.data.returns_info.tagColor,
        receiveName:res.data.returns_info.returnsType,
        receiveType:res.data.returns_info.returnsType=='第三方物流'?1:2,
        refundCarNo:res.data.returns_info.carNO,
        refundPeople:res.data.returns_info.driver,
        refundPhone:res.data.returns_info.driverPhone,
        refundReason:res.data.returns_info.why,
        refundPrice:res.data.returns_info.returnsAmount,
        applyCount:res.data.returns_info.amount,
        applyTime:res.data.returns_info.applyTime.split('T')[0],
        auditWhy:res.data.auditWhy,
        afterNo:res.data.returns_info.returnsNo,
        img1:res.data.returns_info.imageUrl,
        img2:res.data.returns_info.imageUrl2,
        img3:res.data.returns_info.imageUrl3,
        logisticsCompany:res.data.logistics_info.expressNo,
        logisticsNo:res.data.logistics_info.logistics_company,
        ['addressInfo.name']:res.data.address.company_name,
        ['addressInfo.phone']:res.data.address.tel_phone,
        ['addressInfo.address']:res.data.address.address,
        status:res.data.status,
        refundState:res.data.refundState,
        productStatus:res.data.productStatus
      })
      this.getLogisticInfo(res.data.logistics_info.expressNo,res.data.logistics_info.logistics_company)
    })
  },
  getLogisticInfo(){
    let data = {
      returnsId:this.data.returnsId
    }
    console.log(data)
    app.request('wx_app/logistics/getReturnOrderLogistics',data,'GET').then(res=>{
      console.log(res)

      if (res.code==1) {
        if (res.data.Success) {
          console.log(res.data)
          let logisticsList = []
          res.data.Traces.forEach((item,index)=>{
            if (index==0) {
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime,
                isLast:true
              })
            }else if (index==res.data.Traces.length-1) {
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime,
                isNew:true
              })
            }else{
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime
              })
            }
          })
          this.setData({
            logisticsList
          })
        }else{
          // app.errorMsg('没有物流信息')
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  cancelApply(){
    let data = {
      returnsId:this.data.returnsId
    }
    app.request('wx_app/after/sale/cancel',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title:'取消成功',
          success(){
            setTimeout(function(){ 
              wx.navigateBack({
                delta:2
              })
            },1000)
          }
        })
      }
    })
  }
})